﻿using System.Collections.Generic;
using Grpc.Net.Client.Balancer;

namespace Frontend.Balancer;

public class ReportingLoadBalancerFactory : LoadBalancerFactory
{
    private readonly LoadBalancerFactory _loadBalancerFactory;
    private readonly SubchannelReporter _subchannelReporter;

    public ReportingLoadBalancerFactory(LoadBalancerFactory loadBalancerFactory, SubchannelReporter subchannelReporter)
    {
        _loadBalancerFactory = loadBalancerFactory;
        _subchannelReporter = subchannelReporter;
    }

    public override string Name => _loadBalancerFactory.Name;

    public override LoadBalancer Create(LoadBalancerOptions options)
    {
        // Wrap the helper so that state updates can be intercepted.
        // State information is then passed to the reporter.
        var reportingController = new ReportingChannelControlHelper(options.Controller, _subchannelReporter);

        options = new LoadBalancerOptions(reportingController, options.LoggerFactory, options.Configuration);
        return _loadBalancerFactory.Create(options);
    }
}
